<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="引入1234567891011121314151617181. 导入repositories &amp;#123;  mavenCentral()&amp;#125;dependencies &amp;#123;    compile &#39;com.github.bumptech.glide:glide:4.0.0&#39;    annotationProcessor &#39;com.github.bumptech.glide:comp">
<meta property="og:type" content="article">
<meta property="og:title" content="Glide4.0使用">
<meta property="og:url" content="http://yoursite.com/2017/10/20/Glide4/index.html">
<meta property="og:site_name" content="二松同学">
<meta property="og:description" content="引入1234567891011121314151617181. 导入repositories &amp;#123;  mavenCentral()&amp;#125;dependencies &amp;#123;    compile &#39;com.github.bumptech.glide:glide:4.0.0&#39;    annotationProcessor &#39;com.github.bumptech.glide:comp">
<meta property="article:published_time" content="2017-10-20T01:19:30.000Z">
<meta property="article:modified_time" content="2020-06-23T03:08:32.943Z">
<meta property="article:author" content="二松">
<meta property="article:tag" content="android">
<meta property="article:tag" content="glide">
<meta property="article:tag" content="图片">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2017/10/20/Glide4/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>Glide4.0使用 | 二松同学</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">二松同学</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">拥有梦想只是一种智力，实现梦想才是一种能力</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/20/Glide4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="二松">
      <meta itemprop="description" content="good good study day day up.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="二松同学">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Glide4.0使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-10-20 09:19:30" itemprop="dateCreated datePublished" datetime="2017-10-20T09:19:30+08:00">2017-10-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-23 11:08:32" itemprop="dateModified" datetime="2020-06-23T11:08:32+08:00">2020-06-23</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E5%8D%9A%E5%AE%A2/" itemprop="url" rel="index"><span itemprop="name">技术博客</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="引入"><a href="#引入" class="headerlink" title="引入"></a>引入</h2><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 导入</span><br><span class="line">repositories &#123;</span><br><span class="line">  mavenCentral()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">dependencies &#123;</span><br><span class="line">    compile <span class="string">'com.github.bumptech.glide:glide:4.0.0'</span></span><br><span class="line">    annotationProcessor <span class="string">'com.github.bumptech.glide:compiler:4.0.0'</span></span><br><span class="line">    compile <span class="string">'com.android.support:support-v4:25.3.1'</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2.</span> 混淆</span><br><span class="line">如果你有使用到 proguard，那么请把以下代码添加到你的 proguard.cfg 文件中：</span><br><span class="line">-keep <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> * <span class="keyword">implements</span> <span class="title">com</span>.<span class="title">bumptech</span>.<span class="title">glide</span>.<span class="title">module</span>.<span class="title">GlideModule</span></span></span><br><span class="line"><span class="class">-<span class="title">keep</span> <span class="title">public</span> <span class="title">class</span> * <span class="keyword">extends</span> <span class="title">com</span>.<span class="title">bumptech</span>.<span class="title">glide</span>.<span class="title">AppGlideModule</span></span></span><br><span class="line"><span class="class">-<span class="title">keep</span> <span class="title">public</span> <span class="title">enum</span> <span class="title">com</span>.<span class="title">bumptech</span>.<span class="title">glide</span>.<span class="title">load</span>.<span class="title">resource</span>.<span class="title">bitmap</span>.<span class="title">ImageHeaderParser</span>$** &#123;</span></span><br><span class="line">    **[] $VALUES;</span><br><span class="line">    <span class="keyword">public</span> *;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="入门"><a href="#入门" class="headerlink" title="入门"></a>入门</h2><h3 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h3><p>加载</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Glide.with(fragment).load(myUrl).into(imageView);</span><br></pre></td></tr></table></figure>

<p>取消加载</p>
<blockquote>
<p>已经和context的生命周期关联,一般不用调用此方法</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Glide.with(fragment).clear(imageView);</span><br></pre></td></tr></table></figure>

<h3 id="RecyecleView"><a href="#RecyecleView" class="headerlink" title="RecyecleView"></a>RecyecleView</h3><blockquote>
<p>glide会自己判断网址为空的情况(为空清空内容或显示默认图),要注意imageView的复用</p>
<p>复用问题:对 <code>View</code> 调用 <a href="https://muyangmin.github.io/glide-docs-cn/javadocs/400/com/bumptech/glide/RequestManager.html#clear-com.bumptech.glide.request.target.Target-" target="_blank" rel="noopener"><code>clear()</code></a> 或 <code>into(View)</code>，表明在此之前的加载操作会被取消</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onBindViewHolder</span><span class="params">(ViewHolder holder, <span class="keyword">int</span> position)</span> </span>&#123;</span><br><span class="line">	Glide.with(fragment).clear(holder.imageView);</span><br><span class="line">	String url = urls.get(position);</span><br><span class="line">	Glide.with(fragment)</span><br><span class="line">	 	 .load(url)</span><br><span class="line">	 	 .into(holder.imageView);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>GlideApp</p>
<blockquote>
<p>此类可生成出一个流式 API，内联了多种选项，和集成库中自定义的选项</p>
<p>目前仅可以在 Application 模块内使用(在app模块)</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GlideModule</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAppGlideModule</span> <span class="keyword">extends</span> <span class="title">AppGlideModule</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">applyOptions</span><span class="params">(Context context, GlideBuilder builder)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//内存</span></span><br><span class="line">        <span class="keyword">int</span> memoryCacheSizeBytes = <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">20</span>; <span class="comment">// 20mb</span></span><br><span class="line">        builder.setMemoryCache(<span class="keyword">new</span> LruResourceCache(memoryCacheSizeBytes));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//磁盘</span></span><br><span class="line">        <span class="keyword">int</span> diskCacheSizeBytes = <span class="number">1024</span> * <span class="number">1024</span> * <span class="number">100</span>; <span class="comment">// 100 MB</span></span><br><span class="line">        builder.setDiskCache(<span class="keyword">new</span> DiskLruCacheFactory(<span class="string">"cachePic"</span>, diskCacheSizeBytes));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="定义扩展"><a href="#定义扩展" class="headerlink" title="定义扩展"></a>定义扩展</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GlideExtension</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAppExtension</span> </span>&#123;</span><br><span class="line">    <span class="comment">// Size of mini thumb in pixels.</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MINI_THUMB_SIZE = <span class="number">200</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="title">MyAppExtension</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125; <span class="comment">// utility class</span></span><br><span class="line"></span><br><span class="line">    <span class="meta">@GlideOption</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">miniThumb</span><span class="params">(RequestOptions options)</span> </span>&#123;</span><br><span class="line">        options.fitCenter()</span><br><span class="line">                .override(MINI_THUMB_SIZE);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="占位图片"><a href="#占位图片" class="headerlink" title="占位图片"></a>占位图片</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">GlideApp.with(<span class="keyword">this</span>)</span><br><span class="line">  			   .load(url)</span><br><span class="line">                .placeholder(<span class="keyword">new</span> ColorDrawable(Color.BLACK))<span class="comment">//占位</span></span><br><span class="line">                .error(<span class="keyword">new</span> ColorDrawable(Color.RED))<span class="comment">//失败</span></span><br><span class="line">                .fallback(<span class="keyword">new</span> ColorDrawable(Color.GRAY))<span class="comment">//地址为null</span></span><br><span class="line">                .into(iv);</span><br></pre></td></tr></table></figure>

<h3 id="剪裁"><a href="#剪裁" class="headerlink" title="剪裁"></a>剪裁</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"> GlideApp.with(<span class="keyword">this</span>)</span><br><span class="line">                .load(url)</span><br><span class="line">   			   .override(<span class="number">100</span>,<span class="number">100</span>)<span class="comment">//指定像素大小</span></span><br><span class="line">   			   .thumbnail(<span class="number">0.1f</span>)<span class="comment">//缩略图</span></span><br><span class="line">                .circleCrop()<span class="comment">//圆形</span></span><br><span class="line">                .centerInside()<span class="comment">//缩放到两边都小于等于宽或高,不拉伸居中</span></span><br><span class="line">                .fitCenter()<span class="comment">//缩放到两边都小于等于宽或高拉伸居中</span></span><br><span class="line">   			   .centerCrop()<span class="comment">//缩放到两边都大于等于宽或高居中剪裁</span></span><br><span class="line">                .into(iv1);</span><br><span class="line"><span class="comment">//自定义option,配置剪裁</span></span><br><span class="line">GlideOptions options = <span class="keyword">new</span> GlideOptions();</span><br><span class="line">options.circleCrop();</span><br><span class="line">GlideApp.apply(options)</span><br><span class="line"><span class="comment">//glide自带的option</span></span><br><span class="line">GlideApp.apply(RequestOptions.centerCropTransform())</span><br></pre></td></tr></table></figure>

<h3 id="动画"><a href="#动画" class="headerlink" title="动画"></a>动画</h3><blockquote>
<p><code>BitmapTransitionOptions</code>   <code>DrawableTransitionOptions</code></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GlideApp.transition(withCrossFade(<span class="number">1000</span>))</span><br><span class="line">  		   .dontTransform()<span class="comment">//关闭动画</span></span><br></pre></td></tr></table></figure>

<h3 id="监听"><a href="#监听" class="headerlink" title="监听"></a>监听</h3><blockquote>
<p>如果返回true,glide将不再操作.例如:失败不展示失败占位图</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GlideApp.listener(<span class="keyword">new</span> RequestListener&lt;Drawable&gt;() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onLoadFailed</span><span class="params">(@Nullable GlideException e, Object model, Target&lt;Drawable&gt; target, <span class="keyword">boolean</span> isFirstResource)</span> </span>&#123;</span><br><span class="line">                        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onResourceReady</span><span class="params">(Drawable resource, Object model, Target&lt;Drawable&gt; target, DataSource dataSource, <span class="keyword">boolean</span> isFirstResource)</span> </span>&#123;</span><br><span class="line">                        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br></pre></td></tr></table></figure>

<h3 id="优先级"><a href="#优先级" class="headerlink" title="优先级"></a>优先级</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">IMMEDIATE 立即,不管优先级</span></span><br><span class="line"><span class="comment">HIGH</span></span><br><span class="line"><span class="comment">NORMAL</span></span><br><span class="line"><span class="comment">LOW</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">GlideApp.priority(IMMEDIATE)</span><br></pre></td></tr></table></figure>

<h3 id="缓存"><a href="#缓存" class="headerlink" title="缓存"></a>缓存</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">  GlideApp.diskCacheStrategy(DiskCacheStrategy.ALL)<span class="comment">//磁盘缓存模式</span></span><br><span class="line">                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)</span><br><span class="line">                .diskCacheStrategy(DiskCacheStrategy.NONE)</span><br><span class="line">                .onlyRetrieveFromCache(<span class="keyword">true</span>)<span class="comment">//只使用缓存</span></span><br><span class="line">                .skipMemoryCache(<span class="keyword">true</span>)<span class="comment">//跳过内存</span></span><br><span class="line">            <span class="comment">// This method must be called on the main thread.</span></span><br><span class="line">        Glide.get(<span class="keyword">this</span>).clearDiskCache();</span><br><span class="line">        <span class="comment">//不推荐</span></span><br><span class="line">        Glide.get(<span class="keyword">this</span>).clearMemory();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//暂时允许Glide在应用的某些部分使用更多或更少的内</span></span><br><span class="line">Glide.get(<span class="keyword">this</span>).setMemoryCategory(MemoryCategory.LOW);</span><br><span class="line"><span class="comment">// Or:</span></span><br><span class="line">Glide.get(<span class="keyword">this</span>).setMemoryCategory(MemoryCategory.HIGH);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">// This method must be called on the main thread.</span></span><br><span class="line">Glide.get(<span class="keyword">this</span>).clearDiskCache();</span><br><span class="line"><span class="comment">//不推荐</span></span><br><span class="line">Glide.get(<span class="keyword">this</span>).clearMemory();</span><br></pre></td></tr></table></figure>

<h3 id="加载类型"><a href="#加载类型" class="headerlink" title="加载类型"></a>加载类型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">GlideApp.with(<span class="keyword">this</span>).asFile();</span><br><span class="line">GlideApp.with(<span class="keyword">this</span>).asBitmap();</span><br><span class="line">GlideApp.with(<span class="keyword">this</span>).asDrawable();</span><br><span class="line">GlideApp.with(<span class="keyword">this</span>).asGif();</span><br></pre></td></tr></table></figure>

<h3 id="配合okhttp"><a href="#配合okhttp" class="headerlink" title="配合okhttp"></a>配合okhttp</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--Manifest--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">meta-data</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:name</span>=<span class="string">"com.bumptech.glide.integration.okhttp3.OkHttpGlideModule"</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:value</span>=<span class="string">"GlideModule"</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="标记"><a href="#标记" class="headerlink" title="标记"></a>标记</h3><blockquote>
<p>同一个url的问题</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GlideApp.signature(<span class="keyword">new</span> StringSignature(String.valueOf(System.currentTimeMillis())))</span><br></pre></td></tr></table></figure>

<h3 id="下载图片缓存图片"><a href="#下载图片缓存图片" class="headerlink" title="下载图片缓存图片"></a>下载图片缓存图片</h3><blockquote>
<p>获取bitmap，可以对原图进行裁剪和大小指定操作(用于微信分享获取小图)</p>
<p>记得在子线程中执行 InterruptedException ExecutionException 阻塞线程</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">            Bitmap bitmap = GlideApp.with(<span class="keyword">this</span>)</span><br><span class="line">                    .asBitmap()</span><br><span class="line">                    .load(<span class="string">"url"</span>)</span><br><span class="line">                    .centerCrop()</span><br><span class="line">                    .submit(<span class="number">100</span>, <span class="number">100</span>)<span class="comment">//size</span></span><br><span class="line">                    .get();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException | ExecutionException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>缓存图片获得文件对象</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">FutureTarget&lt;File&gt; future = GlideApp.with(<span class="keyword">this</span>)</span><br><span class="line">                .load(<span class="string">"url"</span>)</span><br><span class="line">                .downloadOnly(<span class="number">100</span>, <span class="number">100</span>);<span class="comment">//size 已经废弃不推荐使用</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            File file = future.get();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException | ExecutionException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">或</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">            File file = GlideApp.with(<span class="keyword">this</span>)</span><br><span class="line">                    .asFile()</span><br><span class="line">                    .load(<span class="string">"url"</span>)</span><br><span class="line">                    .centerCrop()</span><br><span class="line">                    .submit(<span class="number">100</span>, <span class="number">100</span>)</span><br><span class="line">                    .get();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException | ExecutionException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>通知图库更新</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">context.sendBroadcast(<span class="keyword">new</span> Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,  </span><br><span class="line">                Uri.fromFile(<span class="keyword">new</span> File(currentFile.getPath()))));</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/android/" rel="tag"># android</a>
              <a href="/blog/tags/glide/" rel="tag"># glide</a>
              <a href="/blog/tags/%E5%9B%BE%E7%89%87/" rel="tag"># 图片</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2017/10/13/Gson/" rel="prev" title="Gson">
      <i class="fa fa-chevron-left"></i> Gson
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2017/12/14/Rxjava1/" rel="next" title="使用Rxjava实现注册登录你真的会了吗">
      使用Rxjava实现注册登录你真的会了吗 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#引入"><span class="nav-number">1.</span> <span class="nav-text">引入</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#入门"><span class="nav-number">2.</span> <span class="nav-text">入门</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本用法"><span class="nav-number">2.1.</span> <span class="nav-text">基本用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RecyecleView"><span class="nav-number">2.2.</span> <span class="nav-text">RecyecleView</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#定义扩展"><span class="nav-number">2.3.</span> <span class="nav-text">定义扩展</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#占位图片"><span class="nav-number">2.4.</span> <span class="nav-text">占位图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#剪裁"><span class="nav-number">2.5.</span> <span class="nav-text">剪裁</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动画"><span class="nav-number">2.6.</span> <span class="nav-text">动画</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监听"><span class="nav-number">2.7.</span> <span class="nav-text">监听</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优先级"><span class="nav-number">2.8.</span> <span class="nav-text">优先级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#缓存"><span class="nav-number">2.9.</span> <span class="nav-text">缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#加载类型"><span class="nav-number">2.10.</span> <span class="nav-text">加载类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配合okhttp"><span class="nav-number">2.11.</span> <span class="nav-text">配合okhttp</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#标记"><span class="nav-number">2.12.</span> <span class="nav-text">标记</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#下载图片缓存图片"><span class="nav-number">2.13.</span> <span class="nav-text">下载图片缓存图片</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="二松"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">二松</p>
  <div class="site-description" itemprop="description">good good study day day up.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">27</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">二松</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  













<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
